package cn.zhaoguang.usedcar.main.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.zhaoguang.usedcar.main.entity.Recommend;
import cn.zhaoguang.usedcar.main.entity.Usedcar;
import cn.zhaoguang.usedcar.main.entity.dto.RecommendDTO;
import cn.zhaoguang.usedcar.main.entity.vo.RecommendVO;
import cn.zhaoguang.usedcar.main.mapper.RecommendMapper;
import cn.zhaoguang.usedcar.main.service.RecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author zhaoguang
 * @Date 2021/7/7 17:40
 */
@Service
public class RecommendServiceImpl implements RecommendService {

    @Autowired
    private RecommendMapper recommendMapper;

    @Override
    public int add(Recommend recommend) {
        Integer exist = recommendMapper.findExistByUsedcarId(recommend.getUsedcarId());
        if (exist != null) {
            //已经存在了，返回0
            return 0;
        }
        recommend.setRecommendTime(DateUtil.date());
        return recommendMapper.insertSelective(recommend);
    }

    @Override
    public int delete(Integer id) {
        return recommendMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<RecommendVO> findAllRecommendForManager() {
        List<RecommendDTO> list = recommendMapper.findAll();
        //处理集合数据
        List<RecommendVO> recommendVO = list.stream().map(u -> {
            RecommendVO recommend = new RecommendVO();
            String name = String.format("%s %s %s %s %s %s", u.getCarBrand(), u.getCarSeries(), u.getCarYear(), u.getCarDisplacement(), u.getCarVersion(), u.getTransmission());
            recommend.setUsedcarTitleName(name);

            BeanUtil.copyProperties(u, recommend);
            return recommend;
        }).collect(Collectors.toList());

        return recommendVO;
    }

    @Override
    public List<Usedcar> findAllRecommendForUser() {
        return recommendMapper.findAllRecommendForUser();
    }
}
